(function() {

	angular.module('cf-content').directive('login', ['$timeout', "$window", function($timeout, $window, EVENT) {
		return {
			restrict: 'AE',
			templateUrl: 'app/login/login.html',
			// replace:true,
			controller: login,
			controllerAs: 'loginCtrl'
		}
	}])

	login.$inject = ['$scope', '$state', '$uibModal','$cookieStore', 'Api','cookieConfig']

	function login($scope, $state, $uibModal,$cookieStore, Api,cookieConfig) {
		var vm = this;
		vm.submited = false;
		vm.user = {
			login_name: '',
			password: ''
		}
		vm.login = function(form) {
			if (form.$invalid) {
				vm.submited = true;
				return;
			} else {
				Api.Login.Post(vm.user).$promise.then(function(data) {
					if (data.code == 0) {

						$cookieStore.put('CFTOKEN', data.data, cookieConfig);

						vm.InfoModel('sm', '登录成功', 'success');
						$state.go('home.citymanage')
					} else {
						vm.InfoModel('sm', '账号名或密码错误', 'error');
					}
				})
			}
		}
		vm.InfoModel = function openModel(size, info, type, callback) {
			var modalInstance = $uibModal.open({
				animation: true,
				templateUrl: 'InfoModel',
				controller: 'InfoModelCtrl',
				size: size,
				resolve: {
					info: function() {
						return info;
					},
					type: function() {
						return type;
					}
				}
			});

			modalInstance.result.then(callback);
		}

	}

})();